package cn.limeizi.shoppingcar.daos.impl;

import cn.limeizi.shoppingcar.MyTools;
import cn.limeizi.shoppingcar.beans.User;
import cn.limeizi.shoppingcar.daos.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author: 李美姿
 * @Date: 2019/7/21 10:38
 * @Version 1.0
 * @描述：
 */
public class UserImpl implements UserDao {
    @Override
    public User login(String userName, String password) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection= MyTools.getConn();
            String sql="select * from student where name=? and password=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,password);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection,preparedStatement,resultSet);
        }
        return null;
    }
}
